package com.skyc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.skyc.dao.ClothesDao;
import com.skyc.dao.liuLanDao;
import com.skyc.entity.Clothes;

import com.skyc.entity.Shop;
import com.skyc.entity.history;
import com.skyc.service.ClothesService;
import com.skyc.service.impl.ClothesServiceImpl;
import com.skyc.util.JDBCUtil;

public class liuLanDaolmpl implements liuLanDao {
	JDBCUtil util = JDBCUtil.getJDBCUtil();

	@Override
	public List<Clothes> BKqueryByID() {
		String sql = "SELECT c_cid FROM t_clothes group by c_cid order by sum(c_sales) desc limit 6";
		CachedRowSet rs = util.query(sql);
		ClothesDao sc=new ClothesDaoImpl();
		List<Clothes> list = sc.againqueryByCcid(rs);
		return list;
	}

	@Override
	public List<Clothes> NZqueryByID(String csex) {
		String sql = "select c_cid from (SELECT * FROM t_clothes where c_sex = '"+csex+"')as a GROUP BY c_cid limit 8";
		CachedRowSet rs = util.query(sql);
		ClothesDao sc=new ClothesDaoImpl();
		List<Clothes> list1 = sc.againqueryByCcid(rs);
		return list1;
	}

	@Override
	public boolean addhistory(int uid,int ccid) {
		String sql = "INSERT into t_history(u_id,c_cid) values (?,?)";
		return util.update(sql, uid, ccid);
	}

	@Override
	public List<Clothes> queryAll(String str) {
		List<Clothes> list = new ArrayList<>();
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '男') {
				String sql = "SELECT c_name FROM (select c_name from t_clothes WHERE c_name like '%男%') as a WHERE c_name like ?";
				CachedRowSet rs = util.query(sql, "%"+str+"%"+"group by c_name");
				try {
					while (rs.next()) {
						Clothes c = new Clothes();
						c.setCcolor(rs.getString("c_color"));
						c.setCid(rs.getInt("c_cid"));
						c.setCimgurl(rs.getString("c_imgurl"));
						c.setCmaintype(rs.getString("c_maintype"));
						c.setCname(rs.getString("c_name"));
						c.setCprice(rs.getInt("c_price"));
						c.setCsales(rs.getInt("c_sales"));
						c.setCsex(rs.getString("c_sex"));
						c.setCsize(rs.getString("c_size"));
						c.setCstock(rs.getInt("c_stock"));
						c.setCtype(rs.getString("c_type"));
						list.add(c);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					util.closeCacheRowSet(rs);
				}
				return list;
			} else if (str.charAt(i) == '女') {
				String sql = "SELECT c_name FROM (select c_name from t_clothes WHERE c_name like '%女%') as a WHERE c_name like ?";
				CachedRowSet rs = util.query(sql, "%"+str+"%"+"group by c_name");
				try {
					while (rs.next()) {
						Clothes c = new Clothes();
						c.setCcolor(rs.getString("c_color"));
						c.setCid(rs.getInt("c_cid"));
						c.setCimgurl(rs.getString("c_imgurl"));
						c.setCmaintype(rs.getString("c_maintype"));
						c.setCname(rs.getString("c_name"));
						c.setCprice(rs.getInt("c_price"));
						c.setCsales(rs.getInt("c_sales"));
						c.setCsex(rs.getString("c_sex"));
						c.setCsize(rs.getString("c_size"));
						c.setCstock(rs.getInt("c_stock"));
						c.setCtype(rs.getString("c_type"));
						list.add(c);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					util.closeCacheRowSet(rs);
				}
				return list;
			} else if(i==str.length()-1 && str.charAt(str.length()-1)!='男' && str.charAt(str.length()-1)!='女'){
				String sql = "select c_name from t_clothes WHERE c_name like ?";
				CachedRowSet rs = util.query(sql, "%"+str+"%"+"group by c_name");
				try {
					while (rs.next()) {
						Clothes c = new Clothes();
						c.setCcolor(rs.getString("c_color"));
						c.setCid(rs.getInt("c_cid"));
						c.setCimgurl(rs.getString("c_imgurl"));
						c.setCmaintype(rs.getString("c_maintype"));
						c.setCname(rs.getString("c_name"));
						c.setCprice(rs.getInt("c_price"));
						c.setCsales(rs.getInt("c_sales"));
						c.setCsex(rs.getString("c_sex"));
						c.setCsize(rs.getString("c_size"));
						c.setCstock(rs.getInt("c_stock"));
						c.setCtype(rs.getString("c_type"));
						list.add(c);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					util.closeCacheRowSet(rs);
				}
				return list;
			}
		}
		return null;
	}

	@Override
	public List<Clothes> queryByType(String csex,String ctype) {
		String sql = "select c_cid from t_clothes where c_sex = ? and c_type=? GROUP BY c_cid";
		CachedRowSet rs = util.query(sql,csex,ctype);
		ClothesDao sc=new ClothesDaoImpl();
		List<Clothes> list = sc.againqueryByCcid(rs);
		return list;
	}

}
